/**
 * 年月选择插件
 * chenjun 20130217
 */
(function($) {
	$.fn.ymcalendar = function(options, param) {
		if (typeof options == "string"){
			return $.fn.ymcalendar.methods[options](this, param);
		}
		options = options || {};
		return this.each(function() {
			var target = $(this);
			if (options.value) target.val(options.value); //只是在传参是判断有value则赋值，后续属性中不处理value属性
			var state = $.data(this, "ymcalendar");
			if (state){
				$.extend(state.options, options);
			} else {
				target.addClass("ymcalendar");
				$.data(this, "ymcalendar", {
					options: $.extend({}, $.fn.ymcalendar.defaults, {
						disabled: (target.attr("disabled") ? true : false),
						onchange: target[0].onchange
					}, options)
				});
				target[0].onchange = null;
				target.removeAttr("disabled");
			}
			create(target);
		});
	};

	$.fn.ymcalendar.defaults = {
		disabled: false
	};

	$.fn.ymcalendar.methods = {
		enable: function(jq) {
			return jq.each(function() {
				setDisabled(this, false);
			});
		},
		disable: function(jq){
			return jq.each(function(){
				setDisabled(this, true);
			});
		},
		setValue: function(jq,val){
			return jq.each(function(){
				this.value = val;
				var state = $.data(this, "ymcalendar");
				if (state.options.onchange) state.options.onchange();
			});
		}
	};

	function createPanel(target,state){
		state.blur = true;
		var v = target.val(), y = v ? v.substr(0,4) : new Date().getFullYear(), m = v ? Number(v.substr(4,2)) : 0;
		var maxY = parseInt((state.options.maxDate||"").substr(0,4)), maxM = parseInt((state.options.maxDate||"").substr(4));
		var box = "<div class='ymcalendar-box' style='left:"+target.offset().left+"px;top:"+(target.offset().top+target.height()+2)+
			"px'><table><thead><tr><td y='-1'>&lt;&lt;</td><th colspan='2' y='"+y+"'>"+y+"年</th><td y='1'"+
			(maxY==y?" class='disable'":"")+">&gt;&gt;</td></tr><thead><tbody><tr>";
		for (var i = 1; i <= 12; i++) {
			if ((i-1)%4==0) box += "</tr>";
			var cls=maxY==y&&maxM<i&&"disable"||m==i&&"selected";
			box += "<td m='"+i+"'"+(cls?" class='"+cls+"'":"")+">"+i+"月</td>";
		}
		box = $.data(target[0],"box",$(box + "</tbody></table></div>").appendTo(document.body));
		var year = box.find("th").mouseup(blurSet).mousedown(function(){state.blur = false;});
		return box.delegate("thead td","mouseup",blurSet).delegate("thead td","mousedown",function(){
			state.blur = false;
			var o = $(this);
			if (o.hasClass("disable")) return;
			var ny = parseInt(year.attr("y")) + parseInt(o.attr("y"));
			if (state.options.maxDate){
				if (maxY==ny) {
					o.addClass("disable");
					box.find("td").each(function(){parseInt(this.getAttribute("m"))>maxM&&$(this).addClass("disable");});
				} else {
					year.next().removeClass("disable");
					box.find("td.disable").removeClass("disable");
				}
			}
			year.attr("y", ny).html(ny + "年");
		}).delegate("tbody td","mouseup",blurSet).delegate("tbody td","mousedown",function(){
			state.blur = false;
			if ($(this).hasClass("disable")) return;
			var mon = this.getAttribute("m");
			mon = mon.length==1?"0"+mon:mon;
			var newval = year.attr("y")+mon;
			box.remove();
			target.removeData("box");
			if (target.val() != newval) {
				target.val(newval);
				if (state.options.onchange) state.options.onchange();
			}
		});
		function blurSet(){
			state.blur = true;
			target[0].focus();
		}
	}
	function create(target) {
		var state = $.data(target[0], "ymcalendar");
		target.attr("readonly","readonly").click(function(){
			if (!target.data("box")) createPanel(target,state);
		}).blur(function(e){ //失去焦点
			var box = target.data("box");
			if (state.blur && box && box.length) {
				box.remove();
				target.removeData("box");
			}
		});
		if (state.options.disabled && !target.attr("disabled")) setDisabled(target[0], true);
		else if (!state.options.disabled && target.attr("disabled")) setDisabled(target[0], false);
	}
	function setDisabled(target, disabled) {
		var state = $.data(target, "ymcalendar");
		state.options.disabled = disabled;
		$(target).attr("disabled",disabled);
		if (disabled) {
			if (target.onclick){
				state.onclick = target.onclick;
				target.onclick = null;
			}
		} else {
			if (state.onclick) target.onclick = state.onclick;
		}
	}
})(jQuery);
$(function(){
	$("head").append("<style>\
			.ymcalendar{border:1px solid #D3D3D3;width:50px;height:28px;line-height:28px;padding:0px 2px;cursor:pointer;font-size:12px;vertical-align:middle;outline:0}\
			.ymcalendar-box{position:absolute;background:#fff;border:1px solid #D3D3D3;padding:1px;overflow:hidden;z-index:9999;display:inline}\
			.ymcalendar-box td,.ymcalendar-box th{height:22px;text-align:center;cursor:default;font-size:12px;border:1px solid transparent;border-radius:3px;min-width:30px;box-sizing:border-box}\
			.ymcalendar-box td:hover{background:#eee}\
			.ymcalendar-box td.selected{border:1px solid #f00}\
			.ymcalendar-box td.disable{color:#ccc}</style>");
	$(".ymcalendar").ymcalendar();
});
